package com.blue.utils;

import java.io.*;
import java.util.Properties;

public class Configuration {

    private Properties pro;
    private FileInputStream inputFile;
    private FileOutputStream outputFile;


    public Configuration() {

    }

    public void voliteUser(String userfile) {

        Properties pps = new Properties();
        try {
            InputStream stream = getClass()
                    .getClassLoader()
                    .getResourceAsStream( userfile );
            BufferedReader br = null;
            br = new BufferedReader( new InputStreamReader( stream, "UTF-8" ) );
            pps.load( br );
            br.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     * <p>初始化Configuration类</p>
     *
     * @param filePath 要读取的配置文件的路径+名称
     */
    public Configuration(String filePath) {
        loadfile( filePath );
        /*pro = new Properties();
        try {
            //读取属性文件  
            inputFile = new FileInputStream( new File( filePath ) );
            System.out.println( "filepath************"+filePath );
            //装载文件  
            pro.load( inputFile );
            inputFile.close();
        } catch (FileNotFoundException e) {
            System.out.println( "读取属性文件--->失败！- 原因：文件路径错误或者文件不存在" );
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println( "装载文件--->失败!" );
            e.printStackTrace();
        }*/
    }

    /**
     * load properties file
     *
     * @param filePath
     */
    public void loadfile(String filePath) {
        pro = new Properties();
        try {
            //读取属性文件
            inputFile = new FileInputStream( new File( filePath ) );
            System.out.println( "filepath************" + filePath );
            //装载文件
            pro.load( inputFile );
            inputFile.close();
        } catch (FileNotFoundException e) {
            System.out.println( "读取属性文件--->失败！- 原因：文件路径错误或者文件不存在" );
            //e.printStackTrace();
            loadfile( filePath );
        } catch (IOException e) {
            System.out.println( "装载文件--->失败!" );
            e.printStackTrace();
        }

    }

    /**
     * <p>得到key值</p>
     *
     * @param key 取得其值的键
     * @return 取得键值
     */
    public String getValue(String key) {
        if (pro.containsKey( key )) {
            String value = pro.getProperty( key );
            return value;
        } else {
            return "";
        }
    }

    /**
     * <p>得到key值</p>
     *
     * @param filePath properties文件的路径+文件名
     * @param key      取得其值的键
     * @return 取得键值
     */
    public String getValue(String filePath, String key) {
        try {
            String value = "";
            inputFile = new FileInputStream( filePath );
            pro.load( inputFile );
            inputFile.close();
            if (pro.contains( key )) {
                value = pro.getProperty( key );
                return value;
            } else {
                return "";
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return "";
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * <p>清除properties文件中所有的key和其值</p>
     */
    public void clear() {
        pro.clear();
    }

    /**
     * <p>改变或添加一个key的值</p>
     * 当key存在于properties文件中时该key的值被value所代替，
     * 当key不存在时，该key的值是value
     *
     * @param key   要存入的键
     * @param value 要存入的值
     */
    public void setValue(String key, String value) {
        pro.setProperty( key, value );
    }

    /**
     * <p>将更改后的文件数据存入指定的文件中，该文件可以事先不存在</p>
     *
     * @param fileName    文件路径+文件名称
     * @param description 对该文件的描述
     */
    public Boolean saveFile(String fileName, String description) {
        try {
            outputFile = new FileOutputStream( new File( fileName ) );
            pro.store( outputFile, description );
            outputFile.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException ioe) {
            ioe.printStackTrace();
            return false;
        }
        return true;
    }

    public Boolean updatePro(String path, String key, String value) {
        System.out.println( "获取添加或修改前的属性值：" + key + "=" + getValue( key ) );
        setValue( key, value );
        Boolean saveFile = saveFile( path, "修改成功" );
        System.out.println( "获取添加或修改后的属性值：" + key + "=" + getValue( key ) );
        return saveFile;
    }

    public Boolean updatePros(String path, String key, String value, String key1, String value1) {
        System.out.println( "获取添加或修改前的属性值：" + key + "=" + getValue( key ) );
        System.out.println( "获取添加或修改前的属性值：" + key1 + "=" + getValue( key1 ) );
        setValue( key, value );
        setValue( key1, value1 );
        Boolean saveFile = saveFile( path, "修改成功" );
        System.out.println( "获取添加或修改后的属性值：" + key + "=" + getValue( key ) );
        System.out.println( "获取添加或修改后的属性值：" + key1 + "=" + getValue( key1 ) );
        return saveFile;
    }

    /**
     * 测试方法
     *
     * @param args
     */
    public static void main(String[] args) {
        String s = "E:\\新建文件夹\\apache-tomcat-8.5.14\\webapps\\mybatisboot-0.0.1\\WEB-INF\\classes\\application.properties";
        Configuration configuration = new Configuration( s );
        /*String value = conf.getValue( "LOCALIP" );
        System.out.println( value );*/
        String value = "jdbc:mysql://" + "127.0.0.1" + ":3306/data_compare?useUnicode=true&characterEncoding=UTF-8&useSSL=false";

        Boolean updatePro = configuration.updatePro( s, "spring.datasource.url", value );
        System.out.println( updatePro );

    }

}
